package org.kitten.core.util;

import java.util.HashSet;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class REUtil {

	/**
	 * 返回匹配的字符串数组[表达式两端不用加起止符号^$,但需要匹配的表达式两端要加小括号].<br>
	 * 如果共3次匹配,每次有两组(),则会返回长度为6的字符串数组.<br>
	 * 
	 * <pre>
	 * for example:
	 *   getREGroupVector(&quot;The three numbers are 3.27,33,4.25.&quot;,&quot;([0-9]+(\\.[0-9]+)?)&quot;)
	 * <br>
	 *   返回 [3.27,.27,33,null,4.25,.25]
	 *   
	 *   getREGroupVector(&quot;The three numbers are 3.27,33,4.25.&quot;,&quot;([0-9]+(?:\\.[0-9]+)?)&quot;)
	 * <br>
	 *   返回 [3.27,33,4.25]
	 * </pre>
	 * 
	 * 本方法会忽略group(0),即entire pattern不会包括在内<br>
	 * 所以要想匹配entire pattern,两头要用括弧包含起来. 提示:<br>
	 * <b>①m.group():</b><br>
	 * Capturing groups are indexed from left to right, starting at one. <br>
	 * Group zero denotes the entire pattern<br>
	 * so the expression m.group(0) is equivalent to m.group().<br>
	 * <b>②m.groupCount()</b><br>
	 * Group zero denotes the entire pattern by convention. It is not included
	 * in this count.
	 * 
	 * @param sentence
	 * @param regex
	 * @return
	 */
	public static String[] getREGroupVector(final String sentence,
			final String regex) {
		Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE
				| Pattern.DOTALL);
		Vector<String> v = new Vector<String>();
		Matcher m = p.matcher(sentence);
		while (m.find()) {
			// groupCount不包含group(0);
			for (int i = 1; i <= m.groupCount(); ++i) {
				v.add(m.group(i));
			}
		}
		String[] a = new String[v.size()];
		v.toArray(a);
		return a;
	}

	/**
	 * 统计某单词出现的次数.
	 * 
	 * @param sentence
	 * @param word
	 * @return
	 */
	public static int getWordCount(final String sentence, final String word) {
		Pattern p = Pattern.compile("\\b(" + word + ")\\b",
				Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
		Matcher m = p.matcher(sentence);
		int count = 0;
		while (m.find()) {
			count++;
		}
		return count;
	}

	/**
	 * 统计某字符串出现的次数.
	 * 
	 * @param sentence
	 * @param str
	 * @return
	 */
	public static int getStrCount(final String sentence, final String str) {
		Pattern p = Pattern.compile(str, Pattern.CASE_INSENSITIVE
				| Pattern.DOTALL);
		Matcher m = p.matcher(sentence);
		int count = 0;
		while (m.find()) {
			count++;
		}
		return count;
	}

	/**
	 * 同getREGroupVector,不同的是会去掉重得的项.
	 * 
	 * @param str
	 * @param regex
	 * @return
	 */
	public static String[] getREGroupSet(final String str, final String regex) {
		Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE
				| Pattern.DOTALL);
		HashSet<String> set = new HashSet<String>();
		Matcher m = p.matcher(str);
		while (m.find()) {
			for (int i = 1; i <= m.groupCount(); ++i) {
				set.add(m.group(i));
			}
		}
		String[] a = new String[set.size()];
		set.toArray(a);
		return a;
	}

	public static boolean matches(final String sentence, final String regex) {
		Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE
				| Pattern.DOTALL);
		Matcher m = p.matcher(sentence);
		return m.matches();
	}

	public static void main(String[] paramArrayOfString) {
		String[] str = REUtil.getREGroupVector(
				"then, this is the end of this file.", ".*(the).*");
		for (int i = 0; i < str.length; i++) {
			System.out.println(str[i]);
		}
	}
}